/** PanelCommonComputed
 * [PanelCommonComputed description]
 * @type {Object}
 * 只放 Panel 必要且共用的 computed / methods
 *             pages[curPanel] => panelLists
 */

import { PanelDatasEditableParamsLists } from '@/views/card/js/data/panelDatas';
import { isArray, isString } from '@/utils/public.js';
import { mapState, mapActions } from 'vuex'
const PanelCommonComputed = {
  computed: {
    // TODO: 分类
    ...mapState({
      panels: state => _.cloneDeep(state.cardComponents.siteData.panels),
      curPanel: state => _.cloneDeep(state.cardComponents.panelComponents.curPanel),
      curPanelData: state => _.cloneDeep(state.cardComponents.panelComponents.curPanelData),
      curPanelTarget: state => _.cloneDeep(state.cardComponents.panelComponents.curTarget),  // 当前选择/操作的 panel item
    }),
    panelLists() {
      return this.curPanelData || [];
    },
    curPanelTargetItem() {
      const list = this.panelLists;
      return this.curSelectPanelIndex > -1 ? list[this.curSelectPanelIndex] : null;
    },
    curSelectPanelIndex() {  /* 获取当前选择/操作的 panel 的索引，不存在则返回 -1 */
      let newIndex = -1;
      const lists = this.panelLists;
      isArray(lists) && lists.filter((item, curIndex) => {
        if(this.curPanelTarget.itemId == item.itemid) {
          newIndex = curIndex;
          return item;
        }
      });

      return newIndex;
    },
    curSelectPanelType() {  /* 获取当前选择/操作的 panel 的类型，无则返回 false */
      const lists = this.panelLists;
      const curSelectPanel = this.curSelectPanelIndex >= 0 ? lists[this.curSelectPanelIndex] : null;
      if(curSelectPanel) return 'Panel_' + curSelectPanel.panel_type;
      return false;
    },
    panelEditableParams() {
      return PanelDatasEditableParamsLists[this.curSelectPanelType] || null;
    },
  },
  methods: {
    ...mapActions({
      handler4AddPanel: 'cardComponents/handler4AddPanel',
      updatePanelListsOriginal: 'cardComponents/panelComponents/updatePanelLists',
      updatePanelCurTarget: 'cardComponents/panelComponents/updateCurTarget',
      updatePanels: 'cardComponents/updatePanels',
    }),
    updatePanelLists(arr) {
      arr = this.setCardItemsIndex(arr);
      this.updatePanelListsOriginal(arr);
    },
    setCardItemsIndex(lists) {  /* list Idx_index 排序 */
      return lists.map((item, index) => {
        item.Idx_index = index.toString();
        return item;
      });
    },
  }
};



export default PanelCommonComputed;
